package com.haiwei.poi.word.test;

import com.haiwei.poi.word.bean.HaiweiBeanUtil;
import com.haiwei.poi.word.common.HaiweiDocumentGenerator;
import com.haiwei.poi.word.loop.*;
import com.haiwei.poi.word.poi.HaiweiXWPFTableUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;

import java.io.File;
import java.util.*;

/**
 * 1.
 *
 * @date: 2022-06-25
 * @version: 1.0
 * @author: karry@haiwei.com
 */
public class test3_back2 {
    
    @Data
    @AllArgsConstructor
    static class Item1{
        private String name = "鼠标";
        private String type = "电子类";
        private String time = "2018/8/26";
    }
    
    @Data
    @AllArgsConstructor
    static class Order1{
        private String name = "订单名称";
        private String no = "订单编号";
        private List<Item1> items = new ArrayList<>();
    }
    
    public static void main(String[] args) {
        try {
            final XWPFDocument document = HaiweiDocumentGenerator
                    .build(new File(FilePathUtils.getPath("33.docx")));
            
            List<Item1> items1 = new ArrayList<>();
            items1.add(new Item1("鼠标11","电子类11","2018/8/11"));
            items1.add(new Item1("鼠标12","电子类12","2018/8/12"));
            
            List<Item1> items2 = new ArrayList<>();
            items2.add(new Item1("鼠标21","电子类21","2018/8/21"));
            items2.add(new Item1("鼠标22","电子类22","2018/8/22"));
            
            Order1 order1 = new Order1("特仑苏1","N0001",items1);
            Order1 order2 = new Order1("特仑苏2","N0002",items2);
            List<Order1> orders = new ArrayList<>();
            orders.add(order1);
            orders.add(order2);
            HaiweiBeanUtil.setBean(orders,"orders");
            HaiweiBeanUtil.setString("N0001文档编号","文档编号");
            HaiweiBeanUtil.print();
            HaiweiBeanUtil.setCallBackCache(null);
            
            // 处理
            for(XWPFTable xwpfTable : document.getTables()){
                // 解析生成haiweiTable
                HaiweiTable haiweiTable = HaiweiTableUtil.getHaiweiTable(xwpfTable);
    
                // 处理haiweiTable
                final List<HaiweiRowGroup> rows = haiweiTable.getRows();
                for(HaiweiRowGroup row : rows){
                    handleRowGroup(row);
                }
                
                //删除模板行
                HaiweiXWPFTableUtil.removeRow(haiweiTable.getTable(),haiweiTable.getStartIndex(),haiweiTable.getEndIndex());
            }
            
            // 生成个文件
            final File file = new File(FilePathUtils.getPath("3333.docx"));
            HaiweiDocumentGenerator.toFile(document,file);
            System.out.println(file.getAbsolutePath());
            
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    
    /**
     * 解析row组
     * @param row
     */
    private static void handleRowGroup(HaiweiRowGroup row) {
        if(row instanceof HaiweiRowGroupBasic){
            HaiweiRowGroupBasic haiweiRow = (HaiweiRowGroupBasic) row;
            handleBasicRowGroup(haiweiRow.getTable(), haiweiRow.getStartIndex());
        }else if(row instanceof HaiweiRowGroupLoop){
            HaiweiRowGroupLoop loop = (HaiweiRowGroupLoop) row;
            // 获取标签
            HaiweiLoopSign loopSign = loop.getLoopSign();
            // 获取循环数量
            int collectionLen = HaiweiBeanUtil.getCollectionLen(loopSign.getPath());

            // 遍历处理, i 是数据下标
            for(int i = 0 ; i < collectionLen ; i++){
                Object obj = HaiweiBeanUtil.getValue(loopSign.getPath() + "." + i);
                HaiweiBeanUtil.setBean(obj,loopSign.getItem());
                List<HaiweiRowGroup> loopRow = loop.getRows();
                // 遍历loop中的行并复制,j是行下标
                for(int j = 0; j < loopRow.size() ; j++){
                    handleRowGroup(loopRow.get(j));
                }
                HaiweiBeanUtil.clean(loopSign.getItem());
            }
        }
    }
    
    /**
     * 处理最基本的数据行
     * @param table
     * @param startIndex
     */
    private static void handleBasicRowGroup(XWPFTable table, int startIndex) {
        int insertIndex = table.getNumberOfRows();
        HaiweiXWPFTableUtil.addAndCopyRows(table
                , table.getRow(startIndex)
                , insertIndex
                , 1);
        HaiweiXWPFTableUtil.parse(table.getRow(insertIndex));
    }
    
}
